﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Configuration;
using DTO;
using Moved;

namespace BUS
{
    public class UserRolesBUS
    {
        private static string _connectstring;
        static UserRolesBUS()
        {
            _connectstring = ConfigurationManager.ConnectionStrings["WebChamCongConnectionString"].ConnectionString.ToString();
        }

        
        public static bool createRoleUser(Int32 UserID)
        {
            try
            {
                using (Moved.Moved_DB dc = new Moved_DB(_connectstring))
                {
                    WIT_UserRoles us = new WIT_UserRoles();
                    us.UserRoles = UserID;
                    us.Actions = "14";
                    dc.WIT_UserRoles.InsertOnSubmit(us);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static List<DTO.UserRoleDTO> getUserRole()
        {
            List<DTO.UserRoleDTO> usr = new List<UserRoleDTO>();
            using (Moved.Moved_DB dc = new Moved_DB(_connectstring))
            {
                var objUserRole = (from us in dc.WIT_UserRoles
                                   select new 
                                   { 
                                       userRoleID=us.ID,
                                       userRole=us.UserRoles,
                                       action=us.Actions
                                   }).Distinct();
                foreach (var usb in objUserRole)
                {
                    DTO.UserRoleDTO kq = new UserRoleDTO();
                    kq.IdUserRole = usb.userRoleID;
                    kq.UserRole = usb.userRole;
                    kq.Action = usb.action.ToString();
                    usr.Add(kq);
                }
                return usr;
            }
        }
        public static DTO.UserRoleDTO getRoleByUserID(int UserID)
        {
            int localuserID = UserID;
            DTO.UserRoleDTO urs = null;
            using (Moved.Moved_DB dc = new Moved_DB(_connectstring))
            {
                var objUserRole = (from us in dc.WIT_UserRoles
                                   where us.UserRoles == localuserID
                                   select new
                                   {
                                       roles = us.Actions
                                   }
                                      ).Distinct();
                foreach (var bb in objUserRole)
                {
                    urs = new UserRoleDTO();
                    urs.Action = bb.roles;
                }
                return urs;
            }
        }
        public static DTO.UserRoleDTO getUserRoleByUserID(int userID)
        {
            int localuserID = userID;
            DTO.UserRoleDTO urs = null;
            using (Moved.Moved_DB dc = new Moved_DB(_connectstring))
            {
                var objUserRole = (from us in dc.WIT_UserRoles
                                   join info in dc.WIT_Roles on Int32.Parse(us.Actions) equals info.ID 
                                   where us.UserRoles == localuserID
                                   select new 
                                   {
                                       roles=us.Actions
                                   }
                                      ).Distinct();
                foreach (var bb in objUserRole)
                {
                    urs = new DTO.UserRoleDTO();
                    urs.Action = bb.roles;
                }
                return urs;
            }
        }
        public static DTO.UserRoleDTO getUserRoleByID(int id)
        {
            DTO.UserRoleDTO urs = null;
            using (Moved.Moved_DB dc = new Moved_DB(_connectstring))
            {
                var objUserRole = (from us in dc.WIT_UserRoles
                                   where us.ID==(int)id
                                   select new
                                   {
                                       userRoleID = us.ID,
                                       userRole = us.UserRoles,
                                       action = us.Actions
                                   }).Distinct();
                foreach (var usb in objUserRole)
                {
                    urs = new UserRoleDTO();
                    urs.IdUserRole = usb.userRoleID;
                    urs.UserRole = usb.userRole;
                    urs.Action = usb.action;
                }
                return urs;
            }
        }
        
    }
}
